<template>
  <basic-dialog
    width="60%"
    title="新增需求"
    :visible="visible"
    :showFooter="false"
    :handleClose="handleCancel"
  >
    <basic-form
      ref="formData"
      isBtnBarSingleRow
      defaultExpandStatus
      :isScalingBtn="false"
      :formColumns="formItems"
      :dataSource="formData"
      :handleSearchForm="handleQueryForm"
      :handleClearForm="clearCategorySelect"
      @getFormData="getFormData"
    >
      <CategoryMultipleSelect
        checkStrictly
        slot="category"
        ref="categoryMultiple"
        :disabledLevel="[1, 2]"
        @selectCategoryCallback="setFormCateUuids"
      />
      <BtnRender slot="extensionActions" :render="renderBtnsBar" />
    </basic-form>
    <SkusTable
      ref="skusTable"
      showSelectCol
      showPagination
      :tableHeight="480"
      :defaultMinWidth="100"
      :dataSource="TableInfo.list"
      :columns="TableInfo.columns"
      :loading="TableInfo.loading"
      :pageSizes="pageSizes"
      :pagination="pagination"
      :total="pagination.total"
      :selectionChange="TableInfo.setSelection"
      @pageChange="handlePageChange"
      @pageSizeSelect="handlePageSizeChange"
    />
  </basic-dialog>
</template>
<script>
import BasicDialog from '@/components/BasicDialog';
import BasicForm from '@/components/BasicForm';
import SkusTable from '@/components/skusTable';
import BtnRender from '@/components/JSXRender/BtnRender';
import CategoryMultipleSelect from '@/components/Category/MultipleSelect';
import skusPage from '@/mixins/commodity/skusPage';
import { FORM_ITEMS, TABLE_COLUMNS, DEV_STATUS_VALUES } from './constants';
import { getInitFormData, TableInfo } from '@/utils/getUtils';
import { getListPageBtnsBar } from '@/utils/getBtnsRender';
import { getPagerService } from '@/api/commodity/developmentRequirementPlan/developRequirementPool';
export default {
  mixins: [skusPage],
  components: {
    BasicDialog,
    BasicForm,
    SkusTable,
    BtnRender,
    CategoryMultipleSelect
  },
  props: {
    visible: {
      type: Boolean,
      default: false
    }
  },
  data () {
    return {
      formItems: FORM_ITEMS,
      formData: getInitFormData(FORM_ITEMS),
      TableInfo: new TableInfo({ columns: TABLE_COLUMNS }),
      pageSizes: [10, 100, 1000]
    }
  },
  watch: {
    visible (val) {
      val && this.getTableData()
    }
  },
  methods: {
    // 渲染按钮操作栏
    renderBtnsBar (h) {
      const btnBars = [
        { label: '批量关联', method: this.handleBatchLinked },
        { label: '取消', type: 'default', method: this.handleCancel }
      ]
      return getListPageBtnsBar(h, btnBars)
    },
    // 取消
    handleCancel () {
      this.$emit('update:visible', false)
    },
    // 批量关联
    handleBatchLinked () {
      this.$emit('getResults', this.TableInfo.selection)
      this.$emit('getDetailNos', this.TableInfo.selection.map(item => item.reqDetailNo))
      this.$emit('update:visible', false)
    },
    // 获取品类集合
    setFormCateUuids (list) {
      this.formData.categoryUuids = list.map(item => item.categoryUuid)
    },
    // 清空品类选择信息
    clearCategorySelect () {
      this.$refs.categoryMultiple.clearSelectResult()
    },
    // 获取表格数据
    async getTableData () {
      const data = {
        ...this.formData,
        devStatusList: [DEV_STATUS_VALUES.ToDevelop, DEV_STATUS_VALUES.NotDevelop],
        ...this.paginationParams
      }
      const res = await getPagerService(data, this.TableInfo.setLoading)
      if (res?.retData) {
        this.TableInfo.setList(res.retData?.results)
        this.setTotalNum(res.retData.totalNum)
      }
    }
  }
}
</script>
